package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Override
    public List<Setmeal> findCategoryById(ArrayList<Long> ids) {
       return setmealMapper.findCategoryById(ids);
    }

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        System.out.println("套餐id" + setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 加入套餐id的SetmealDish
        List<SetmealDish> setmealDishes1 = setmealDishes.stream().map(setmealDish -> setmealDish.setSetmealId(setmeal.getId())).collect(Collectors.toList());
        // 插入茶品名称
        setmealDishService.addSetmealDishes(setmealDishes1);

    }

    @Override
    public SetmealVO querySetmealById(Long id) {
        Setmeal setmeal = setmealMapper.querySetmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        Category dishCateGoryById = categoryService.findDishCateGoryById(setmeal.getCategoryId());
        List<SetmealDish> setmealDishes = setmealDishService.queryMealDishesBySetmealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        setmealVO.setCategoryName(dishCateGoryById.getName());
        return setmealVO;
    }

    @Override
    public PageResult querySetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmeals = setmealMapper.querySetmealPage(setmealPageQueryDTO);
        System.out.println(setmeals);
        PageResult pageResult = new PageResult(setmeals.getTotal(), setmeals.getResult());
        return pageResult;
    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 删除菜品
        setmealDishService.delSetmealDishBySetmealId(setmeal.getId());


        // 重新插入菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(setmealDish -> setmealDish.setSetmealId(setmeal.getId())).collect(Collectors.toList());
        setmealDishService.addSetmealDishes(collect);

        // 更新菜品
        setmealMapper.updateSetmeal(setmeal);
    }
}
